<?php

if (!session_is_registered('user')) exit();

$q = str_replace(" ", "%", $_GET['q']);
$library = $_GET['library'];
$page = $_GET['page'];
$sort = $_GET['sort'];
$f = $_GET['f'];
$artist = $_GET['artist'];
$album = $_GET['album'];
$edit = $_GET['edit'];

$db = sqlite_open($gen['path'] . "\\library\\". $lib['file']);


## Get Tags (New in 0.2)

$sql_tags = sqlite_query($db, "SELECT * FROM tags");

while ($tags = sqlite_fetch_array($sql_tags)) {
  if ($tags['tag_name'] == "Genre")     $tag_genre = $tags['tag_col'];
  if ($tags['tag_name'] == "Album")     $tag_album = $tags['tag_col'];
  if ($tags['tag_name'] == "Artist")    $tag_artist = $tags['tag_col'];
  if ($tags['tag_name'] == "Track")     $tag_track = $tags['tag_col'];
}



if ((!$album) && (!$artist) && (!$edit)) {
  if (!$sort) $sort = "albums";

  ## Top of page, with search

  echo "<center>\n";
  echo "<b style=\"font-size: 24; margin-top: 20px;\">".$lib['Name']."</b>\n";

  ## Sort by


  echo "<table width=\"100%\">\n";
  echo "<tr>\n";
  echo "<td align=\"center\">\n";
  if ($sort != "albums") echo "<a href=\"index.php?section=library&library=$library&sort=albums\">\n";
  else echo "<b>\n";
  echo "Sort by Albums\n";
  if ($sort != "albums") echo "</a>\n";
  else echo "</b>\n";
  echo "</td>\n";

  echo "<td align=\"center\">\n";
  if ($sort != "artists") echo "<a href=\"index.php?section=library&library=$library&sort=artists\">\n";
  else echo "<b>\n";
  echo "Sort by Artists\n";
  if ($sort != "albums") echo "</a>\n";  
  else echo "</b>\n";
  echo "</td>\n";
  echo "</tr>\n";
  echo "</table>\n";

  ## Search

  echo "<br><br>\n";
  echo "<form method=\"GET\" action=\"index.php?section=library&library=$library\">\n";
  echo "<b>Search $sort:</b>\n";
  echo "<input type=\"text\" name=\"q\" size=\"10\" value=\"".str_replace("%", " ",$q)."\">\n";
  echo "<input type=\"hidden\" name=\"library\" value=\"$library\">\n";
  echo "<input type=\"hidden\" name=\"section\" value=\"library\">\n";
  echo "<input type=\"hidden\" name=\"sort\" value=\"$sort\">\n";
  echo "<input type=\"submit\" value=\"GO\">\n";
  echo "</form>\n";
  echo "<br>\n";


  ## Display search-string (if any)

  if ($q) echo "<i>Searching for: </i>\"".str_replace("%", " ",$q)."\"<br><br>";

  if ($sort == "albums") {

    $sql = sqlite_query($db, "SELECT * FROM items WHERE $tag_album LIKE '%$q%' GROUP BY $tag_album ORDER BY $tag_album");
    $results = sqlite_num_rows($sql);

    $limit = $lib['pagelimit'];

    if (!$page) $page = 1;
    if ($limit != 0) {
      $offset = (($page * $limit) - $limit);
      $pages = ($results / $limit);
      $string = "LIMIT $limit OFFSET $offset";
    }
    else {
      $offset = 0;
      $pages = 1;
    }



    ## Display Page navigation TOP

    if ($page > 1) echo "<a href=\"index.php?section=library&library=$library&page=". ($page - 1) ."&q=$q\">";
    echo "&lt;&lt;Previous";
    if ($page > 1) echo "</a>";
    echo " ";
    for ($i=1; $i <= ceil($pages); $i++) {
      echo "  ";
      if ($i != $page) echo "<a href=\"index.php?section=library&library=$library&page=$i&q=$q\">";
      else echo "<b>";
      echo $i;
      if ($i != $page) echo "</a>";
      else echo "</b>";
    }
    echo " ";
    if ($page < $pages) echo "<a href=\"index.php?section=library&library=$library&page=". ($page + 1) ."&q=$q\">";
    echo "Next&gt;&gt;";
    if ($page < $pages) echo "</a>";


    echo "<table width=\"100%\" align=\"center\" style=\"margin-top: 20px;\">\n";
    $sql = sqlite_query($db, "SELECT * FROM items WHERE $tag_album LIKE '%$q%' GROUP BY $tag_album ORDER BY $tag_album $string");

    for ($i = 1; $data = sqlite_fetch_array($sql); $i++) {

      $id = $data['item_id'];

      if ($i == 1) echo "<tr>\n";

      echo "<td valign=\"top\" align=\"center\" style=\"padding: 5px; padding-bottom: 20px;\">\n";
      echo "<a href=\"index.php?section=library&library=$library&album=$id\">\n";
      if ($data['item_image']) echo "<img src=\"thumb.php?id=$id&library=$library\" style=\"border-color: black;\"><br>\n";
      else echo "<img src=\"images/noimage.jpg\" width=\"150\" height=\"150\" style=\"border-color: black;\"><br>\n";

      echo $data[$tag_album];
      echo "</a>\n";
      echo "</td>\n";

      if ($i == 3) {
        $i = 0;
        echo "</tr>\n";
      }
    }

    echo "</table>\n";
  }

  elseif ($sort == "artists") {
    if (!$f) $f = "A";


    ## Display Page navigation TOP

    $letters = array(1=>"a");

    echo "<center>";
    for ($i = "A"; $i <= "Y"; $i++) {
      echo " ";
      if ($f != $i) echo "<a href=\"index.php?section=library&library=$library&sort=artists&f=$i\">";
      else echo "<b>";
      echo $i;
      if ($f != $i) echo "</a>\n";
      else echo "</b>\n";
    }
    echo " ";
    if ($f != "Z") echo "<a href=\"index.php?section=library&library=$library&sort=artists&f=Z\">";
    else echo "<b>";
    echo "Z";
    if ($f != "Z") echo "</a>";
    else echo "</b>";
    echo " - ";
    if ($f != "%") echo "<a href=\"index.php?section=library&library=$library&sort=artists&f=%\">";
    else echo "<b>";
    echo "All";
    if ($f != "%") echo "</a>";
    else echo "</b>";

    echo "</center>";
    if ($q) $f = "%$q";

    $sql = sqlite_query($db, "SELECT * FROM items WHERE $tag_artist LIKE '$f%' GROUP BY $tag_artist ORDER BY abs($tag_artist) ASC");
    echo "<table width=\"100%\" align=\"center\" style=\"margin-top: 20px;\">\n";
    while ($data = sqlite_fetch_array($sql)) {
      $id = $data['item_id'];
      echo "<tr>\n";
      echo "<td style=\"padding-left: 10px;\">";
      echo "<a href=\"index.php?section=library&library=$library&artist=$id\">\n";
      echo $data[$tag_artist];
      echo "</a>\n";
      echo "</td>\n";

      echo "</tr>\n";
    }

    echo "</table>\n";
  }
  echo "</center>";
}

elseif ($artist) {
  $sql = sqlite_query($db, "SELECT * FROM items WHERE item_id='$artist'");
  $data = sqlite_fetch_array($sql);
  $name = $data[$tag_artist];
  if (!$sort) $sort = "Song";


  ## Where am I?
  $first = substr($data[$tag_artist],0,1);
  echo "<a href=\"index.php?section=library\">Library</a> -&gt; \n";
  echo "<a href=\"index.php?section=library&library=$library&sort=artists&f=$first\">".$lib['name']."</a> -&gt; \n";
  echo "<b>".$data[$tag_artist]."</b>\n";


  echo "<table width=\"100%\" style=\"margin-bottom: 20px;\">\n";

  echo "<tr><td colspan=\"2\" align=\"center\" style=\"font-size: 24; padding-bottom: 30px;\"><b>Artist: $name</b></td></tr>\n";

  if (session_is_registered('admin')) {
    echo "<tr>\n";
    echo "<td align=\"center\" style=\"padding-bottom: 20px;\">\n";
    echo "<button onclick=\"window.open('do.php?what=play&type=artist&action=play&library=$library&id=$artist&sort=$sort','do')\">";
    echo "Play Artist</button>\n";
    echo "</td>\n";
    echo "<td align=\"center\" style=\"padding-bottom: 20px;\">\n";
    echo "<button onclick=\"window.open('do.php?what=play&type=artist&action=queue&library=$library&id=$artist&sort=$sort','do')\">";
    echo "Queue Artist</button>\n";
    echo "</td>\n";
    echo "</tr>\n";
  }
  echo "</table>\n";

  echo "<table width=\"100%\" style=\"margin-bottom: 20px;\">\n";
  echo "<tr>\n";
  echo "<td width=\"45%\" style=\"border-bottom: 2px solid black;\">\n";
  echo "<a href=\"index.php?section=library&library=$library&artist=$artist&sort=Song\"><b>Song</b></a>\n";
  if ($sort == "Song") {
    echo " -&gt;";
    $sql = sqlite_query($db, "SELECT * FROM items WHERE $tag_artist='$name' ORDER BY item_name ASC");
  }
  echo "</td>\n";

  echo "<td width=\"45%\" style=\"padding-left: 20px; border-bottom: 2px solid black;\">\n";
  echo "<a href=\"index.php?section=library&library=$library&artist=$artist&sort=Album\"><b>Album</b></a>\n";
  if ($sort == "Album") {
    echo " -&gt;";
    $sql = sqlite_query($db, "SELECT * FROM items WHERE $tag_artist='$name' ORDER BY $tag_album ASC, item_name ASC");
  }
  echo "</td>\n";
  echo "<td width=\"10%\" style=\"border-bottom: 2px solid black;\"><b>Actions</b>*</td>\n";
  echo "</tr>\n";

  
  while ($data = sqlite_fetch_array($sql)) {
    $id = $data['item_id'];
    echo "<tr>\n";
    echo "<td width=\"40%\" style=\"border-bottom: 1px solid black;\">".$data['item_name']."</td>\n";
    echo "<td width=\"40%\" style=\"padding-left: 20px; border-left: 1px solid black; border-bottom: 1px solid black;\">\n";
    echo "<a href=\"index.php?section=library&library=$library&album=$id\">";
    echo $data[$tag_album];
    echo "</a>";
    echo "</td>\n";
    echo "<td width=\"20%\" style=\"border-left: 1px solid black; border-bottom: 1px solid black;\">\n";
    if (session_is_registered('admin')) {
      echo "<button onclick=\"window.open('do.php?what=play&type=music&action=play&library=$library&id=$id','do')\">P</button>\n";
      echo "<button onclick=\"window.open('do.php?what=play&type=music&action=queue&library=$library&id=$id','do')\">Q</button>\n";
    }
    ## Streaming
    if ($lib['streaming'] == "1") {
      $open = "window.open('player.php?library=$library&id=$id&type=music', 'Music', 'feature=status=0,toolbar=0,location=0,directories=0,menubar=0,scrollbars=0,resizable=1,width=650,height=800');";
      echo "<button onclick=\"$open\">S</button>\n";
      echo "<button onclick=\"window.open('do.php?what=qs&id=$id','do')\">A</button>\n";
    }
    if (session_is_registered('admin')) {
      echo "<button onclick=\"location.href = 'index.php?section=library&library=$library&edit=$id&from=artist'\">E</button>";
    }
    echo "</td>\n";
    echo "</tr>\n";
  }


  echo "</table>\n";
  if (session_is_registered('admin')) {
    echo "[P] = Plays file, cleares existing queue!<br>\n";
    echo "[Q] = Queue file in existing queue<br>\n";
  }
  if ($lib['streaming'] == 1) {
    echo "[S] = Stream file, cleares existing queue!<br>\n";
    echo "[A] = Add file to existing stream-queue!<br>\n";
  }
  if (session_is_registered('admin')) {
    echo "[E] = Edit Tags<br>\n";
  }
}

elseif ($album) {
  $album = addslashes($album);
  $sql = sqlite_query($db, "SELECT * FROM items WHERE item_id='$album'");
  $data = sqlite_fetch_array($sql);
  $name = $data[$tag_album];
  $image = $data['item_image'];


  ## Where am I?
  $sql_cnt = sqlite_query($db, "SELECT item_id FROM items GROUP BY $tag_album ORDER BY $tag_album ASC");
  for ($i = 1; $cnt = sqlite_fetch_array($sql_cnt); $i++) {
    if ($cnt['item_id'] == $data['item_id']) $number = $i;
  }
  $where = ceil($number / $lib['pagelimit']);
  echo "<a href=\"index.php?section=library\">Library</a> -&gt; \n";
  echo "<a href=\"index.php?section=library&library=$library&page=$where\">".$lib['name']."</a> -&gt; \n";
  echo "<b>".$data[$tag_album]."</b>\n";
  
  
  echo "<table width=\"100%\" style=\"margin-bottom: 20px; margin-top: 20px;\">\n";

  echo "<tr><td colspan=\"2\" align=\"center\" style=\"font-size: 24; padding-bottom: 20px;\"><b>Album: $name</b></td></tr>\n";

  if ($image) {  
    echo "<tr>\n";
    echo "<td colspan=\"2\" align=\"center\" style=\"padding-bottom: 20px;\">\n";
    echo "<img src=\"pic.php?id=$album&library=$library\">\n";
    echo "</td>\n";
    echo "</tr>\n";
  }
  echo "</table>\n";

  if (session_is_registered('admin')) {
    echo "<table align=\"center\" width=\"70%\" style=\"margin: 20px;\">";
    echo "<tr>";
    echo "<td align=\"center\">";
    echo "<button onclick=\"window.open('do.php?what=play&type=album&action=play&library=$library&id=$album','do')\">Play Album</button>";
    echo "</td>";
    echo "<td align=\"center\">";
    echo "<button onclick=\"window.open('do.php?what=play&type=album&action=queue&library=$library&id=$album','do')\">Queue Album</button>";
    echo "</td>";
    echo "</table>";
  }

  echo "<table width=\"100%\" border=\"1\" style=\"margin-bottom: 20px; margin-top: 20px;\">\n";
  echo "<tr>\n";
  echo "<td width=\"15\"><b>#</b></td>\n";
  echo "<td><b>Artist</b></td>\n";
  echo "<td><b>Title</b></td>\n";
  echo "<td><b>Actions:</b></td>\n";
  echo "</tr>\n";

  $sql = sqlite_query($db, "SELECT * FROM items WHERE $tag_album=\"$name\" ORDER BY abs($tag_track)");
  while ($data = sqlite_fetch_array($sql)) {
    $id = $data['item_id'];
    echo "<tr>\n";
    echo "<td width=\"15\" align=\"right\">".$data[$tag_track]."</td>\n";
    echo "<td>";
    echo "<a href=\"index.php?section=library&library=$library&artist=$id\">";
    echo $data[$tag_artist];
    echo "</a>";
    echo "</td>\n";
    echo "<td>".$data['item_name']."</td>\n";
    echo "<td align=\"right\">";
    if (session_is_registered('admin')) {
      echo "<button onclick=\"window.open('do.php?what=play&type=music&action=play&library=$library&id=$id&from=album','do')\">P</button>";
      echo "<button onclick=\"window.open('do.php?what=play&type=music&action=queue&library=$library&id=$id&from=album','do')\">Q</button>";
    }
    ## Streaming
    if ($lib['streaming'] == "1") {
      $open = "window.open('player.php?library=$library&id=$id&type=music', 'Music', 'feature=status=0,toolbar=0,location=0,directories=0,menubar=0,scrollbars=0,resizable=1,width=650,height=720');";
      echo "<button onclick=\"$open\">S</button>\n";
      echo "<button onclick=\"window.open('do.php?what=qs&id=$id&library=$library','do')\">A</button>\n";
    }
    if (session_is_registered('admin')) {
      echo "<button onclick=\"location.href = 'index.php?section=library&library=$library&edit=$id'\">E</button>";
    }
    echo "</td>";
    echo "</tr>\n";
  }

  echo "</table>\n";
  if (session_is_registered('admin')) {
    echo "[P] = Plays file, cleares existing queue!<br>\n";
    echo "[Q] = Queue file in existing queue<br>\n";
  }
  if ($lib['streaming'] == 1) {
    echo "[S] = Stream file, cleares existing queue!<br>\n";
    echo "[A] = Add file to existing stream-queue!<br>\n";
  }
  if (session_is_registered('admin')) {
    echo "[E] = Edit Tags<br>\n";
  }
}
elseif ($edit) {
    $sql = sqlite_query($db, "SELECT * FROM items WHERE item_id='$edit'");
    $data = sqlite_fetch_array($sql);
    
    $title = $data['item_name'];
    $artist = $data[$tag_artist];
    $album = $data[$tag_album];
    $track = $data[$tag_track];
    
    $from = $_GET['from'];
    
    echo "<b style=\"font-size: 24;\">Edit Song</b><br>\n";
    echo "<form method=\"post\" action=\"do.php?what=updatesong&id=$edit&library=$library&from=$from\">\n";
    echo "<table width=\"100%\" border=\"1\" style=\"margin-top: 20px;\">\n";
    echo "<tr>\n";
    echo "<td width=\"50%\"><b>Title</b></td>\n";
    echo "<td width=\"50%\">\n";
    echo "<input type=\"text\" name=\"title\" value=\"$title\">\n";
    echo "</td>\n";
    echo "</tr>\n";
    echo "<tr>\n";
    echo "<td width=\"50%\"><b>Artist</b></td>\n";
    echo "<td width=\"50%\">\n";
    echo "<input type=\"text\" name=\"artist\" value=\"$artist\">\n";
    echo "</td>\n";
    echo "</tr>\n";
    echo "<tr>\n";
    echo "<td width=\"50%\"><b>Album</b></td>\n";
    echo "<td width=\"50%\">\n";
    echo "<input type=\"text\" name=\"album\" value=\"$album\">\n";
    echo "</td>\n";
    echo "</tr>\n";
    echo "<tr>\n";
    echo "<td width=\"50%\"><b>Track number</b></td>\n";
    echo "<td width=\"50%\">\n";
    echo "<input type=\"text\" name=\"track\" value=\"$track\">\n";
    echo "</td>\n";
    echo "</tr>\n";
    echo "</table>\n";
    echo "<center><input type=\"submit\" value=\"Update\"></center>";
    echo "</form>";
}
?>
